package model.database;

import java.util.HashMap;
import java.util.Map;

import model.data.MapArray;
import model.data.MapMap;
import model.data.MapSortedMap;
import model.data.Result;
import model.search.Keywords;

public class DatabaseContent {
	private MapMap adsInCategory;

	private MapMap adsInProvince;

	private MapMap adsInCounty;

	private MapMap ads;

	private MapArray countiesInProvince;

	private MapArray categoriesInCategory;

	private HashMap<String, String> subCategoryOfCategory;

	private DatabaseContent() {
		ads = new MapMap();
		adsInCounty = new MapMap();
		adsInProvince = new MapMap();
		adsInCategory = new MapMap();
		countiesInProvince = new MapArray();
		categoriesInCategory = new MapArray();
		subCategoryOfCategory = new HashMap<String, String>();
	}

	public MapMap getAdsInCategory() {
		return adsInCategory;
	}

	public MapMap getAdsInCounty() {
		return adsInCounty;
	}

	public MapMap getAdsInProvince() {
		return adsInProvince;
	}

	public MapArray getCategoriesInCategory() {
		return categoriesInCategory;
	}

	public MapMap getAds() {
		return ads;
	}
	
	public void putAd(String ad, String content) {
		//System.out.println("-------"+ad+","+content+"--------");
		for (String keyword : new Keywords(content)) {
			ads.put(ad, keyword);
			//System.out.println("ads.put(\""+ad+"\",\""+keyword+"\")");
			//System.out.println("ads.get(\""+ad+"\") = "+ads.get(ad));
		}

		//ads.put("key", "subkey");
		//System.out.println("ad: "+ads.get("key"));
	}

	public void putAdInCounty(String province, String county, String ad) {
		adsInCounty.put(county, ad);
		countiesInProvince.put(province, county);
	}

	public void putAdInProvince(String province, String ad) {
		adsInProvince.put(province, ad);
	}

	public void putAdInCategory(String category, String ad, String enddate) {
		adsInCategory.put(category, ad, enddate);
	}

	public void putSubCategoryInCategory(String category, String subCategory) {
		categoriesInCategory.put(category, subCategory);
	}

	public MapArray getCountiesInProvince() {
		return countiesInProvince;
	}

	public static DatabaseContent create() {
		DatabaseConnection database = new DatabaseConnection();
		database.connect();
		DatabaseContent databaseContent = new DatabaseContent();
		database.loadDatabaseContent(databaseContent);
		databaseContent.calculateAdsIn("0");
		database.disConnect();
		return databaseContent;
	}

	private HashMap<String, String> calculateAdsIn(String category) {
		HashMap<String, String> sum = new HashMap<String, String>();
		if (adsInCategory.containsKey(category))
			sum.putAll(adsInCategory.get(category));
		if (categoriesInCategory.containsKey(category))
			for (String subCategory : categoriesInCategory.get(category)) {
				sum.putAll(calculateAdsIn(subCategory));
			}
		adsInCategory.putAll(category, sum);
		return sum;
	}

	public void setCategoryOfSubCategory(String subCategory, String category) {
		subCategoryOfCategory.put(subCategory, category);
	}

	public String getParentCategoryOf(String category) {
		return subCategoryOfCategory.get(category);
	}
}
